package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Address;
import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
public class Usercontroller {
@RequestMapping("/ss")
    public String aa(){
        System.out.println(159);
        return "success.jsp";
    }
    @RequestMapping("/ss1")
    public String aa1(@RequestParam(name="username") String name, Integer age){
        System.out.println(name+","+age);
        return "success.jsp";
    }
    @RequestMapping("/ss2")
    public  String aa2(User user){
        System.out.println(user);
        return "success.jsp";
    }

    @RequestMapping("/ss3")
    public String aa3(User user){
        System.out.println(user);
    return "success.jsp";
    }
    @RequestMapping("/ss4")
    public String aa4(User user){
        System.out.println(user);
    return "success.jsp";
    }

    @RequestMapping("/ss5")
    public String aa5(User user){
        System.out.println(user);
        return "success.jsp";
    }
    @RequestMapping("/ss6")
    public String aa6(@DateTimeFormat(pattern ="yyyy-MM-dd") Date date){
        System.out.println(date);
        return "success.jsp";
    }

    @RequestMapping("/ss7")
    public String aa7(){

        return "forward:success.jsp";
    }

    @RequestMapping("/ss8")
    public String aa8(){

        return "redirect:success.jsp";
    }

    //使用ModelAndView形参传递参数，该对象还封装了页面信息
    @RequestMapping("/ss9")
    public ModelAndView aa9(ModelAndView modelAndView){
        Address address=new Address("安徽","合肥");
        modelAndView.addObject("address",address);
        modelAndView.addObject("ni",15);
        modelAndView.setViewName("WEB-INF/success.jsp");
        return modelAndView;
    }

    @RequestMapping("ss10")
    @ResponseBody
    public String josn() throws JsonProcessingException {
    Address address=new Address("安徽","合肥");
        ObjectMapper om=new ObjectMapper();
        String string = om.writeValueAsString(address);
        return string;
    }
}
